package com.workflow.workflowproject.listener;

import com.workflow.workflowproject.entity.FlowLogs;
import com.workflow.workflowproject.mapper.FlowLogsMapper;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 任务结束监听
 */
@Slf4j
@Component
public class TaskCompletedListener implements FlowableEventListener {
    @Autowired
    private FlowLogsMapper flowLogsMapper;

    private static TaskCompletedListener taskCompletedListener;

    @Autowired
    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    @PostConstruct
    public void init(){
        taskCompletedListener = this;
        taskCompletedListener.processEngine =this.processEngine;
        taskCompletedListener.flowLogsMapper =this.flowLogsMapper;
    }

    @Override
    public void onEvent(FlowableEvent event) {
        log.info(" -------------------------------------------任务结束时监听-------------------------start");
        FlowableEntityEvent engineEvent = (FlowableEntityEvent)event;
        Object entity = engineEvent.getEntity();
        TaskEntityImpl task = (TaskEntityImpl) entity;
        ProcessInstance processInstance = processEngine.getRuntimeService().createProcessInstanceQuery()
                .processInstanceId(task.getProcessInstanceId())
                .singleResult();

        log.info("进入类型:" + event.getType());
        log.info("流程实例ID:" + task.getProcessInstanceId());
        log.info("流程业务ID：" + processInstance.getBusinessKey());
        log.info("任务nodeKey：" + task.getTaskDefinitionKey());
        log.info("任务ID：" + task.getName());
        log.info("任务实例ID：" + task.getId());

        FlowLogs flowLogs = new FlowLogs();
        flowLogs.setId(task.getId());
        flowLogs.setType(event.getType().toString());
        flowLogs.setBusKey(processInstance.getBusinessKey());
        flowLogs.setProcessInstanceId(task.getProcessInstanceId());
        flowLogs.setNodeKey(task.getTaskDefinitionKey());
        flowLogs.setTaskName(task.getName());
        flowLogs.setResult("流程通过");
        flowLogs.setUserId("user");
        taskCompletedListener.flowLogsMapper.insert(flowLogs);

    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return "";
    }
}
